/**
 * 管理评论的实现类
 */
package com.tjetc.service.Impl;

import com.tjetc.dao.CommentMapper;
import com.tjetc.dao.DishMapper;
import com.tjetc.dao.MerchantMapper;
import com.tjetc.entity.Comment;
import com.tjetc.entity.Dish;
import com.tjetc.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private DishMapper dishMapper;

    /***
     * 菜品id查询评论
     * @param dishId
     * @return
     */
    @Override
    public List<Comment> selectByDishId(int dishId) {
        return commentMapper.selectByDishId(dishId);
    }

    /***
     * 添加评论信息
     * @param comment
     * @return
     */
    @Override
    public int addComment(Comment comment) {
        return commentMapper.addComment(comment);
    }

    /***
     * 用户id查询评论
     * @param userId
     * @return
     */
    @Override
    public List<Comment> selectByUserId(int userId) {
        return commentMapper.selectByUserId(userId);
    }

    /***
     * 根据id删除评论
     * @param id
     * @return
     */
    @Override
    public int deleteById(int id) {
        return commentMapper.deleteById(id);
    }

    /***
     * 商家id查询评论
     * @param merchantId
     * @return
     */
    @Override
    public List<Comment> queryByMerchantId(int merchantId) {
        List<Comment> list=new ArrayList<>();
        //批量查询商家菜品
        List<Dish> dishes = dishMapper.selectByMerchantId(merchantId);
        //批量查询菜品评论
        for(int i=0;i<dishes.size();i++){
            List<Comment> comments = commentMapper.selectByDishId(dishes.get(i).getId());
            System.out.println(comments);
            list.addAll(comments);
        }
        return list;
    }
}
